from datetime import date

from odoo import fields, models, _
from odoo.exceptions import UserError


class OperatingStatementsWizard(models.TransientModel):
    _name = 's.operating.statements.share.wizard'
    _description = 'Operating Statements Share Wizard'

    month = fields.Selection([
        ('1', '一月'),
        ('2', '二月'),
        ('3', '三月'),
        ('4', '四月'),
        ('5', '五月'),
        ('6', '六月'),
        ('7', '七月'),
        ('8', '八月'),
        ('9', '九月'),
        ('10', '十月'),
        ('11', '十一月'),
        ('12', '十二月'),
    ], string='Month', required=True)

    line_ids = fields.One2many('s.operating.statements.share.line.wizard', 'order_id')

    def default_get(self, fields_list):
        result = super().default_get(fields_list)

        operating_statements = self.env['s.operating.statements']
        line_vals_list = []

        items = {
            'production_wages': "生产工资",
            'quality_wages': "品质工资",
            'technical_department_wages': "技术部工资",
            'production_electricity_usage': "生产使用电",
            'production_water_usage': "生产使用水",
            'production_gas_usage': "生产使用燃气",
            'finished_goods_warehouse': "成品仓",
            'chemicals_warehouse': "药水仓",
            'raw_materials_warehouse': "物料仓",
            'sales_expenses': "销售费用",
            'variable_expenses': "变动销费",
            'business_salaries_and_bonus': "业务薪资&奖金",
            'transportation_fees': "运输费",
            'fixed_indirect_labor': "固定间接人工",
            'meals_and_accommodation': "餐费/住宿费",
            'welfare_expenses': "福利费",
            'bonus_accrual': "奖金计提",
            'depreciation_and_amortization': "折旧/摊销",
            'maintenance_and_repair_fees': "维修保养费用",
            'fixed_utilities': "固定水电费",
            'environmental_protection_fees': "环境保护费",
            'communication_fees': "通讯费",
            'company_vehicle_expenses': "公司车辆",
            'research_and_development_expenses': "研发费用",
            'entertainment_and_travel_expenses': "招待及差旅费",
            'other_fixed_expenses': "其它固定费用",
            'non_operating_income_expenses': "营业外收支",
            'financial_expenses': "财务费用",
            'exchange_gain_loss': "兑换盈亏"
        }
        for business_type, name in items.items():
            line_vals_list.append((0,0, {
                'name': name,
                'business_type': business_type
            }))
        # for name, item in operating_statements.fields_get().items():
        #     if item.get('type', '') == 'float':
        #         line_vals_list.append((0, 0, {
        #             'name': item.get('string', ''),
        #             'business_type': name
        #         }))
        result['line_ids'] = line_vals_list
        return result

    def action_confirm(self):
        today = date.today()
        month = int(self.month)
        if month >= today.month:
            raise UserError(_('It is not allowed to allocate records greater than or equal to the current month'))

        s_date = today.replace(month=month, day=1)
        first_day, last_day, days = self.env['s.operating.statements'].get_month_range_with_days(s_date)
        records = self.env['s.operating.statements'].search([('s_date', '>=', first_day), ('s_date', '<=', last_day)])
        for line in self.line_ids.filtered(lambda x: x.allocated_amount > 0):
            amount = line.allocated_amount / days
            for rec in records:
                setattr(rec, line.business_type, getattr(rec, line.business_type, 0) + amount)


class OperatingStatementsLineWizard(models.TransientModel):
    _name = 's.operating.statements.share.line.wizard'
    _description = 'Operating Statements Share Wizard'

    order_id = fields.Many2one('s.operating.statements.share.wizard')
    name = fields.Char('Name')
    business_type = fields.Char('business type')
    allocated_amount = fields.Float('Allocated amount', default=0.0)